//
//  VisitorTableViewController.swift
//  WeiboDemo
//
//  Created by Quentin Zang on 2020/3/13.
//  Copyright © 2020 臧乾坤. All rights reserved.
//

import UIKit

class VisitorTableViewController: UITableViewController {

    ///  用户登录标记
    private var userLogin = true
    
    ///  访问视图
    var visitorView: VisitorView?//  访客视图不能使用懒加载，否则访客视图始终都会被创建出来
   
    override func loadView() {
        print("come here")
        //super.loadView()
        
        // 根据用户登录情况，决定显示的跟视图
        userLogin ? super.loadView() : setupVisitorView()
    }
    
    ///  设置访客视图 - 让每一个'小'函数
    private func setupVisitorView() {
        // 替换跟试图
        visitorView  = VisitorView()
        view = visitorView
       
        // 添加监听方法
        visitorView?.registerButton.addTarget(self, action: #selector(visitorViewDidRegister), for: .touchUpInside)
        visitorView?.loginButton.addTarget(self, action: #selector(visitorVIewDidLogin), for: .touchUpInside)
        
                
        // 设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(visitorViewDidRegister))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(visitorVIewDidLogin))
    }
    
}

// MARK: - 访客视图监听方法
extension VisitorTableViewController {
    @objc func visitorViewDidRegister() {
        print("注册")
    }
    
    @objc func visitorVIewDidLogin() {
        print("登录")
        let vc = OAuthViewController()
        let nav = UINavigationController(rootViewController: vc)
        nav.modalPresentationStyle = .fullScreen
        present(nav, animated: true, completion: nil)
    }
    
    
}
